import React, { Component, Fragment } from "react";
// antd
import { Table, Button, Col, Pagination, Form, Select, Typography, Space, Tag, Input } from "antd";

// http
import http from '../../../utils/request';

const { Text, Link } = Typography;
const { Option } = Select;


function ListItem(props) {
    return <div><Link href="">{props.value}</Link></div>
}

class NasHistory extends Component {
    constructor(props){
        super(props)
        this.state = {
            loading: false,

            // 分页
            total: '',
            page: 1,
            page_number: 10,

            // 搜索
            search_instance_id: null,
            search_mount_domain: null,
            search_business: null,
            search_instance_platform: null,
            search_instance_account_name: null,
            search_option_account: [],
            search_option_platform: [],
            label_list_option: [],
            label_list_option_name: [],
            label_nas_list: null,

            visable_label: false,
            loading_label: false,

            selectedRowKeys: [],


            // 表格
            dataSource: [],
            columns: [
                {title: "实例ID/名称", dataIndex: "instance_id", key: "instance_id",
                    render: (text, rowData) => {
                        return(
                            <Space direction="vertical">
                                <Link href="">
                                    {rowData.instance_id}
                                </Link>
                                <Text >{rowData.name}
                                </Text>
                            </Space>
                        )
                    }
                },
                {title: "云平台", dataIndex: "cloud-platform", key: "cloud-platform",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="green">{rowData.platform}-{rowData.account_name}</Tag>                                
                            </div>
                        )
                    }
                },
                {title: "区域", dataIndex: "region", key: "region",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="green">{rowData.region}</Tag>
                            </div>
                        )
                    }
                },
                {title: "事业部", dataIndex: "division", key: "division",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="green">{rowData.division}</Tag>
                            </div>
                        )
                    }
                },
                {title: "业务标签", dataIndex: "business", key: "business",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="green">{rowData.business}</Tag>
                            </div>
                        )
                    }
                },
                {title: "业务域名", dataIndex: "mount_domain", key: "mount_domain", width: "360px",
                    render: (text, rowData) => {
                        const listItems = rowData.mount_domain.map((item) => 
                                <ListItem key={item.toString()} value={item} />
                        );
                        return (
                            <ul>{listItems}</ul>
                        )
                    }
                },
                {title: "实例类型", dataIndex: "instance_type", key: "instance_type"},

                {title: "已用空间(GB)", dataIndex: "storage_used", key: "storage_used", defaultSortOrder: 'descend', sorter: (a, b) => a.storage_used - b.storage_used,
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="green">{rowData.storage_used}</Tag>                                
                            </div>
                        )
                    }
                },
                {title: "存储空间(GB)", dataIndex: "storage_total", key: "storage_total", defaultSortOrder: 'descend', sorter: (a, b) => a.storage_total - b.storage_total,
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="blue">{rowData.storage_total}</Tag>                                
                            </div>
                        )
                    }
                },
                {title: "使用率(%)", dataIndex: "utilization_rate", key: "utilization_rate", defaultSortOrder: 'descend', sorter: (a, b) => a.utilization_rate - b.utilization_rate,
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="blue">{rowData.utilization_rate}</Tag>                                
                            </div>
                        )
                    }
                },
            ],
        }
        this.formRefLabel = React.createRef();
    }

    componentDidMount() {
        this.loadData()
        this.loadDataPlatform()
        this.loadDataBusiness()
    }

    loadDataBusiness = () => {
        const requestDataBusiness = {
            'page': 1,
            'limit': 1000,
        }
        http.get('/business', {params: requestDataBusiness}).then(response=>{
            const resp_data = response.data.data.items
            const label_list_option = []
            const label_list_option_name = []
            for (let i = 0; i < resp_data.length; i++) {
                label_list_option.push(<Option key={resp_data[i].id}>{resp_data[i].name}</Option>)
                label_list_option_name.push(<Option key={resp_data[i].name}>{resp_data[i].name}</Option>)

            }
            this.setState({
                label_list_option: label_list_option,
                label_list_option_name: label_list_option_name
            })
        })
    }

    loadDataPlatform = () => {
        const requestData = {
            'page': 1,
            'limit': 1000,
        }
        http.get('/cloud/platform', {params: requestData}).then(response=>{
            const search_option_platform = []
            const resp_data = response.data.data.items
            for (let i = 0; i < resp_data.length; i++) {
                search_option_platform.push(<Option key={resp_data[i].name}>{resp_data[i].name}</Option>)
            }
            this.setState({
                search_option_platform: search_option_platform
            })
        })
    }

    loadData = () => {
        this.setState({
            loading: true
        })
        const requestData = {
            'page': this.state.page,
            'limit': this.state.page_number,
            'deleted': true,
            'instance_id__contains': this.state.search_instance_id,
            'mount_domain__contains': this.state.search_mount_domain,
            'business': this.state.search_business,
            'platform': this.state.search_instance_platform,
            'account_name': this.state.search_instance_account_name,
        }
        // 获取列表
        http.get('/cloud/nas', {params: requestData}).then(response=>{
            this.setState({
                dataSource: response.data.data.items,
                total: response.data.data.pages.total,
                loading: false
            })
        }).catch()
    }

    // 切换每页数量
    onChangeSizePage = (page, page_number) => {
        this.setState({
            page: page,
            page_number: page_number,
        }, () => {
            this.loadData()
        })
    }

     /** 搜索 */
    onFinishSearch = (data) => {
        this.setState({
            search_instance_id: data.instance_id ? data.instance_id : null,
            search_mount_domain: data.mount_domain ? data.mount_domain : null,
            search_instance_platform: data.platform,
            search_instance_account_name: data.account_name,
            search_business: data.business,
            page: 1
        })
        this.loadData()
    }

    // 搜索切换云平台
    onChangePlatform = (value) => {
        if (value) {
            this.setState({
                search_instance_platform: value
            })

            const requestData = {
                'page': 1,
                'limit': 1000,
                'platform': value
            }
    
            http.get('/cloud/account', {params: requestData}).then(response=>{
                const search_option_account = []
                const resp_data = response.data.data.items
                for (let i = 0; i < resp_data.length; i++) {
                    search_option_account.push(<Option key={resp_data[i].name}>{resp_data[i].name}</Option>)
                }
                this.setState({
                    search_option_account: search_option_account
                })
            })

        } else {
            this.setState({
                search_option_account: [],
            })
        }
    }

    // 搜索云账户
    onChangeAccountName = (value) => {
        this.setState({
            search_instance_account_name: value ? value : null
        })
    }

    // 搜索业务线
    onChangeBusiness = (value) => {
        this.setState({
            search_business: value ? value : null
        })
    }

    // 批量选中标签
    onSelectChange = (selectedRowKeys) => {
        this.setState({
            selectedRowKeys: selectedRowKeys
        })
    }

    render(){
        const { loading, columns, dataSource, page, total, search_option_platform, search_option_account, selectedRowKeys, label_list_option_name,
        } = this.state
        const rowSelection = {
            selectedRowKeys,
            onChange: this.onSelectChange,
        }
        var myStyle = {margin: '20px'}
        var myStyle1 = {margin: '10px', float: 'right'}
        return (
            <Fragment>
                <Form layout="inline" onFinish={this.onFinishSearch}>

                    <Form.Item label="实例ID" name="instance_id">
                        <Input placeholder="实例ID"  />
                    </Form.Item>     

                    <Form.Item label="挂载域名" name="mount_domain">
                        <Input placeholder="挂载域名" />
                    </Form.Item>       

                    <Form.Item label="云平台" name="platform">
                        <Select
                            allowClear
                            showSearch
                            style={{ width: 200 }}
                            placeholder="请选择区域"
                            optionFilterProp="children"
                            onChange={this.onChangePlatform}
                            filterOption={(input, option) =>
                            option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                            }
                        >
                            {search_option_platform}
                        </Select>
                    </Form.Item>

                    <Form.Item label="云账户" name="account_name">
                        <Select
                            allowClear
                            showSearch
                            style={{ width: 200 }}
                            placeholder="请选择云账户"
                            onChange={this.onChangeAccountName}
                            >
                            {search_option_account}
                        </Select>                      
                    </Form.Item>

                    <Form.Item label="标签搜索" name="business">
                        <Select
                            allowClear
                            showSearch
                            style={{ width: 180 }}
                            placeholder="请选择区域"
                            optionFilterProp="children"
                            onChange={this.onChangeBusiness}
                            filterOption={(input, option) =>
                            option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                            }
                        >
                            {label_list_option_name}
                        </Select>
                    </Form.Item>
     
                    <Form.Item shouldUpdate={true}>
                        <Button type="primary" htmlType="submit">搜索</Button>
                    </Form.Item>
                </Form>

                <div style={myStyle1}></div>


                <Table
                    pagination={false} 
                    rowSelection={rowSelection} 
                    rowKey="id" 
                    columns={columns} 
                    dataSource={dataSource}
                    loading={loading}
                    scroll={{ y: 600, x: '70vw' }}
                >
                </Table>

                <div style={myStyle}>
                    <Col span={16}>
                        <Pagination
                            onChange={this.onChangeSizePage}
                            current={page}
                            total={total}
                            showSizeChanger
                            showQuickJumper
                            showTotal={total => `Total ${total} items`}
                        />
                    </Col>
                </div>

            </Fragment>
        )
    }
}

export default NasHistory;